<?php

namespace app\index\controller;

use app\common\utils\Constant;
use app\index\logic\BacteriaLogic;
use app\common\controller\Frontend;

/**
 * Created by PhpStorm.
 * User: Orange
 * Date: 2018/5/5
 * Time: 16:47
 */
class Bacteria extends Frontend
{
    /**
     * 游客搜索
     * @return \think\response\Json
     */
    public function ajax_user_search()
    {
        $result = $this->check();
        if (!$result['success']) {
            return $this->returnJson($result['msg'], $result['code']);
        }
        $data['keyword'] = $this->request->post('keyword');
        if ($data['keyword'] == "") {
            return $this->returnJson("关键词不能为空", Constant::CODE_ERROR, "关键词不能为空");
        }
        $logic = new BacteriaLogic();
        $result = $logic->UserSearchByKeyword($data['keyword']);
        if ($result['status'] == 1) {
            return $this->returnJson('', Constant::CODE_SUCCESS, $result['data']);
        } else {
            return $this->returnJson('获取失败!', Constant::CODE_ERROR, $result['error']);
        }

    }

    /**
     * 会员搜索
     * @return \think\response\Json
     */
    public function ajax_member_search()
    {
        $result = $this->check();
        if (!$result['success']) {
            return $this->returnJson($result['msg'], $result['code']);
        }
        $data['keyword'] = $this->request->post('keyword');
        $data['ip'] = $this->request->ip();
        $token = $this->request->param('token');
        if ($data['keyword'] == "") {
            return $this->returnJson("关键词不能为空", Constant::CODE_ERROR, "关键词不能为空");
        }
        $logic = new BacteriaLogic();
        $result = $logic->MemberSearchByKeyword($data['keyword'], $data['ip'], $token);
        if ($result['status'] == 1) {
            return $this->returnJson('', Constant::CODE_SUCCESS, $result['data']);
        } else {
            return $this->returnJson('获取失败!', Constant::CODE_ERROR, $result['error']);
        }
    }

    /**
     * 提交菌种
     * @return \think\response\Json
     */
    public function ajax_bacteria_submit()
    {
        $result = $this->check();
        if (!$result['success']) {
            return $this->returnJson($result['msg'], $result['code']);
        }
        $data['bacteria_id'] = $this->request->post('bacteria_id');
        $token = $this->request->param('token');
        $logic = new BacteriaLogic();
        $result = $logic->sumbitBacteria($data['bacteria_id'], $token);
        if ($result['status'] == 1) {
            return $this->returnJson('', Constant::CODE_SUCCESS, $result['data']);
        } else {
            return $this->returnJson('获取失败!', Constant::CODE_ERROR, $result['error']);
        }
    }

    /**
     * 修改菌种
     * @return \think\response\Json
     */
    public function ajax_bacteria_edit()
    {
        $result = $this->check();
        if (!$result['success']) {
            return $this->returnJson($result['msg'], $result['code']);
        }
        $data['bacteria_name'] = $this->request->post('bacteria_name');
        $data['lading_name'] = $this->request->post('lading_name');
        $data['category'] = $this->request->post('category/a');
        if ($data['bacteria_name'] == "") {
            return $this->returnJson("中文名不能为空", Constant::CODE_ERROR, "中文名不能为空");
        }
        if ($data['lading_name'] == "") {
            return $this->returnJson("拉丁文不能为空", Constant::CODE_ERROR, "拉丁文名不能为空");
        }
        if (!preg_match("/^[a-zA-Z0-9\s]+$/", $data['lading_name'])) {
            return $this->returnJson("请输入正确的拉丁文", Constant::CODE_ERROR, "请输入正确的拉丁文");
        }
        if (sizeof($data['category']) != 7) {
            return $this->returnJson("分类数据有误", Constant::CODE_ERROR, "分类数据有误");
        }
        for ($level = 0; $level <= 6; $level++) {
            if (!is_numeric($data['category'][$level])) {
                return $this->returnJson("分类数据有误", Constant::CODE_ERROR, "分类数据有误");
            }
        }
        $token = $this->request->param('token');
//        return $this->returnJson('获取失败!', Constant::CODE_ERROR, $data);
        $logic = new BacteriaLogic();
        $result = $logic->editBacteria($data, $token);
        if ($result['status'] == 1) {
            return $this->returnJson('', Constant::CODE_SUCCESS, $result['data']);
        } else {
            return $this->returnJson('获取失败!', Constant::CODE_ERROR, $result['error']);
        }
    }
    public function ajax_get_bacteria()
    {
        $result = $this->check();
        if (!$result['success']) {
            return $this->returnJson($result['msg'], $result['code']);
        }
        $data['bacteria_id'] = $this->request->post('bacteria_id');
        $token = $this->request->param('token');
        $logic = new BacteriaLogic();
        $result = $logic->getBacteria($data['bacteria_id'], $token);
        if ($result['status'] == 1) {
            return $this->returnJson('', Constant::CODE_SUCCESS, $result['data']);
        } else {
            return $this->returnJson('获取失败!', Constant::CODE_ERROR, $result['error']);
        }
    }
}